package com.gxa.community.customer.service.mine.impl;

import com.alibaba.fastjson.JSONObject;
import com.gxa.community.core.pojo.vo.order.AfterSalesVo;
import com.gxa.community.core.pojo.vo.order.OrderInfoVo;
import com.gxa.community.core.utils.R;
import com.gxa.community.customer.config.RedisUtils;
import com.gxa.community.customer.service.mine.MineService;
import com.gxa.community.customer.util.RangeUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class MineServiceImpl implements MineService {

    private static final String ADMIN_IP = "http://127.0.0.1:8080";

    final RedisUtils redisUtils;

    public MineServiceImpl(RedisUtils redisUtils) {
        this.redisUtils = redisUtils;
    }

    /**
     * 查询『我的』界面基础信息
     *
     * @param uid 用户编号
     */
    @Override
    public R getMineInfo(Integer uid) {
        return get(uid, "/mine/getMineInfo");
    }

    @Override
    public R queryCoupon(Integer uid) {
        return get(uid, "/mine/queryCoupon");
    }

    @Override
    public R queryOrder(Integer page, Integer size, Integer uid, Integer status) {
        HashMap<String, String> param = new HashMap<>();
        param.put("page", String.valueOf(page));
        param.put("size", String.valueOf(size));

        String s = RangeUtils.get(ADMIN_IP + "/mine/queryOrder",
                new OrderInfoVo().setBuyerId(uid).setStatus(status), null, param
        );
        return JSONObject.parseObject(s, R.class);
    }

    @Override
    public R queryAfterSales(Integer page, Integer size, Integer uid) {
        HashMap<String, String> param = new HashMap<>();
        param.put("page", String.valueOf(page));
        param.put("size", String.valueOf(size));

        String s = RangeUtils.get(ADMIN_IP + "/mine/queryAfterSales",
                new AfterSalesVo().setUserId(uid), null, param
        );
        return JSONObject.parseObject(s, R.class);
    }

    @Override
    public R addAfterSales(AfterSalesVo salesVo) {
        String s = RangeUtils.post(ADMIN_IP + "/mine/addAfterSales",
                salesVo, null, null
        );
        return JSONObject.parseObject(s, R.class);
    }

    @Override
    public R queryGoods(Integer uid, List<String> orderNo) {
        HashMap<String, String> param = new HashMap<>();
        param.put("uid", String.valueOf(uid));

        String s = RangeUtils.get(ADMIN_IP + "/mine/queryGoods",
                null, null, param
        );
        return JSONObject.parseObject(s, R.class);
    }

    private static R get(Integer uid, String url) {
        HashMap<String, String> param = new HashMap<>();
        param.put("uid", String.valueOf(uid));

        String s = RangeUtils.get(ADMIN_IP + url,
                null, null, param
        );
        return JSONObject.parseObject(s, R.class);
    }
}
